package com.yyt.module_main.vm;

import android.databinding.ObservableField;
import android.text.TextUtils;
import android.view.View;

import com.vondear.rxtool.RxRegTool;
import com.yyt.libsrc.bean.LoginCallbackBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;
import com.yyt.module_main.R;

import top.lixb.libcommon.binding.BindingViewModel;

public class LoginVM extends BindingViewModel {
    public ObservableField<String> mobile = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();
    public ObservableField<String> uname = new ObservableField<>();
    public ObservableField<String> headIcon = new ObservableField<String>();

    /**
     * 忘记密码
     * @param view
     */
    public void forgetPwd(View view) {
        RouterHelper.getInstance().startAct(RouteTable.main_forget_pwd);
    }


    /**
     * 注册账号
     * @param view
     */
    public void toRegister(View view) {
        RouterHelper.getInstance().startAct(RouteTable.main_register);
    }

    /**
     * 登录
     * @param view
     */
    public void loginNow(View view) {
        if (!RxRegTool.isMobileSimple(mobile.get())) {
            MsgHelper.showToast("手机号码不正确");
            return;
        }
        if (TextUtils.isEmpty(pwd.get())) {
            MsgHelper.showToast("密码不能为空");
            return;
        }

        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.appLogin)
                .addP("phone", mobile.get())
                .addP("loginPwd",pwd.get())
                .build(), new SuccessCallback<LoginCallbackBean>() {
            @Override
            public void onSuccess(LoginCallbackBean loginCallbackBean) {
                LocalHelper.saveTicket(loginCallbackBean.data.ticket);
                RouterHelper.getInstance().startAct(RouteTable.main_index);
                finish();
                if (null!=mActivity) {
                    mActivity.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                }
            }
        });
    }

    /**
     * 清空输入
     * @param view
     */
    public void clearInput(View view) {
        mobile.set("");
    }

}
